package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginAppBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.AppLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api("APP登录管理")
@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {
    @Resource
    private AppLoginService appLoginService;

    @ApiOperation("微信小程序登录")
    @GetMapping("/wxLogin")
    public AjaxResult minLogin(String code,String encryptedData, String iv) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
      //  AppUser appUser = getLoginAppUser().getAppUser();
        String token = appLoginService.wxLogin(code,encryptedData,iv);
        ajax.put(Constants.TOKEN, token);
      //  ajax.put("appUser", appUser);
        return ajax;
    }
    @ApiOperation("微信小程序登录")
    @GetMapping("/checkLogin")
    public AjaxResult checkLogin(String code) {
        String token = appLoginService.checkLogin(code);
        AjaxResult ajax = AjaxResult.success();
        if(StringUtils.isNotEmpty(token)){
          //  AppUser appUser = getLoginAppUser().getAppUser();
            ajax.put(Constants.TOKEN, token);
            //ajax.put("appUser", appUser);
            return ajax;
        }
        return AjaxResult.success();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/userInfo")
    @Anonymous
    public AjaxResult userInfo() {
        AppUser appUser = getLoginAppUser().getAppUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("appUser", appUser);
        return ajax;
    }

    /**
     * @param encryptedData
     * @param sessionKey
     * @param iv
     * @return
     */
    @GetMapping("/savePhone")
    public AjaxResult savePhone(String encryptedData, String iv, String sessionKey){
        return AjaxResult.success(appLoginService.savePhone(encryptedData,iv,sessionKey));
    }
}
